<template>
  <section>

    		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item class="set_width">
					<el-input v-model="filters.orgName" placeholder="机构名称"></el-input>
				</el-form-item>
				<el-form-item class="set_width">
					<el-input v-model="filters.address" placeholder="地址"></el-input>
				</el-form-item>
				<el-form-item class="set_width">
					<el-input v-model="filters.principalName" placeholder="机构负责人"></el-input>
				</el-form-item>
				<el-form-item class="set_width">
					<el-input v-model="filters.contactName" placeholder="机构联系人"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryUsers">查询</el-button>
					<el-button type="success" v-on:click="resetquery">清空</el-button>
				</el-form-item>
			</el-form>
		</el-col>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" v-on:click="handleAdd('new')">添加机构</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table
      :data="users"
      highlight-current-row
      v-loading="listLoading"
      @selection-change="selsChange"
      style="width: 100%;">
      <el-table-column prop="id" label="ID" width="55"></el-table-column>
      <el-table-column prop="orgName" label="单位名称"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="tel" label="电话"></el-table-column>
      <el-table-column prop="principalName" label="负责人"></el-table-column>
      <el-table-column prop="principalMobile" label="负责人电话"></el-table-column>
      <el-table-column prop="contactName" label="联系人"></el-table-column>
      <el-table-column prop="contactMobile" label="联系人电话"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleOrgEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="10"
        :total="total"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!--增加机构-->
    <el-dialog title="机构管理" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="90px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单位名称" prop="orgName">
              <el-input v-model="addForm.orgName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="tel">
              <el-input v-model="addForm.tel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="addForm.address"></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="负责人" prop="principalName">
              <el-input v-model="addForm.principalName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人电话">
              <el-input v-model="addForm.principalMobile"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人">
              <el-input v-model="addForm.contactName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人电话">
              <el-input v-model="addForm.contactMobile"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="editLoading">保存</el-button>
      </div>
    </el-dialog>
 
  </section>
</template>

<script>
import util from "../../common/js/util";
//import NProgress from 'nprogress'
import {
  getUserListPage,
  removeUser,
  batchRemoveUser,
  editUser,
  addUser
} from "../../api/api";

export default {
  data() {
    return {
      filters: {
        orgName: "",
        address:"",
        principalName:"",
        contactName:""
      },
      isedit: false, //是否是编辑模式，因为调用的是一个编辑框
      users: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [], //列表选中列 
      rejuectFormVisible: false, // 拒绝页面是否显示
      editLoading: false,

      textarea3: "", // 通过原因
      textarea4: "", // 拒绝原因
      addFormVisible: false, //新增界面是否显示
      addLoading: false,
      //新增界面数据
      addForm: {
        orgName: "",
        tel: "",
        address: "",
        contactName: "",
        contactMobile: "",
        principalName: "",
        principalMobile: ""
			},
			addFormRules:{
				orgName: [{required: true, message:'请输入机构名称', trigger:'blur'}],
        tel: [{required: true, message:'请输入机构联系电话', trigger:'blur'}],
				address: [{required: true, message:'请输入机构地址', trigger:'blur'}],
				principalName: [{required: true, message:'请输入负责姓名',trigger:'blur'}]
			}
		};
  },
  methods: {
    //性别显示转换
    formatSex: function(row, column) {
      return row.sex == 1 ? "男" : row.sex == 0 ? "女" : "未知";
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getUsers();
    },
    //获取用户列表
    getUsers() {
      this.listLoading = true;
      //NProgress.start();
      let  useroption = {
					pageNum:this.page,
					pageSize:10,
					status: 0,
					delFlag:0,
					orgName: this.filters.orgName,
					address: this.filters.address,
					principalName: this.filters.principalName,
					contactName: this.filters.contactName
				};
      this.axios
        .get("/referee/manage/org",{params: useroption})
        .then(res => {
          this.total = res.data.data.total;
          this.users = res.data.data.list;
          this.listLoading = false;
        })
        .catch(err => {
          console.log(err);
        });

      // getUserListPage(para).then((res) => {
      // 	this.total = res.data.total;
      // 	this.users = res.data.users;
      // 	this.listLoading = false;
      // 	//NProgress.done();
      // });
    },
    // 查询裁判员
    queryUsers(){
				this.page = 0;
				this.getUsers();

			},
    resetquery(){
				// 清空搜索条件，重新查询所有裁判员
				this.filters = {
					orgName: '',
					address:'',
					principalName: '',
					contactName:''
				};
				this.page = 0;
				this.getUsers();
			},
    //拒绝
    handleDel: function(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row.id };
					removeOrgs(row.id).then((res) => {
						this.listLoading = false;
						if(res.data.code === '0000'){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						this.getUsers();
						}
						
					});
				}).catch(() => {

				});
    },
    handleOrgEdit: function(index, row) {
      this.isedit = true;
      this.addFormVisible = true;
      this.addForm = {
        orgName: row.orgName,
        tel: row.tel,
        address: row.address,
        contactName: row.contactName,
        contactMobile: row.contactMobile,
        principalName: row.principalName,
        principalMobile: row.principalMobile,
        id: row.id
      };
    },
    //显示新增界面
    handleAdd: function() {
      this.isedit =false;
      this.addFormVisible = true;
      delete this.addForm.id;
      this.addForm = {
        orgName: "",
        tel: "",
        address: "",
        contactName: "",
        contactMobile: "",
        principalName: "",
        principalMobile: ""
      };
    },
    //新增以及编辑
    addSubmit: function() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.addLoading = true;
						//NProgress.start();
						if(this.isedit){
              this.axios.put('/referee/manage/org',JSON.parse(JSON.stringify(this.addForm)))
              .then((res) => {
                this.editLoading = false;
                if(res.data.code=== '0000'){
                  this.$message({
                    message: "操作成功",
                    type: "success"
                  });
                  this.$refs.addForm.resetFields();
                  this.addFormVisible = false;
                  this.getUsers();
                }else{
                    this.$message({
                    message: res.data.msg,
                    type: "warning"
                  });
                }


              }).catch((err) => {
                
              });
              
            }else{
              this.axios.post('/referee/manage/org',JSON.parse(JSON.stringify(this.addForm)))
              .then((res)=>{
                this.editLoading = false;
                if(res.data.code=== '0000'){
                  this.$message({
                    message: "操作成功",
                    type: "success"
                  });
                  this.$refs.addForm.resetFields();
                  this.addFormVisible = false;
                  this.getUsers();
                }else{
                    this.$message({
                    message: res.data.msg,
                    type: "warning"
                  });
                }
						}).catch((err)=>{

            })
            }
						
            


          });
        }else{
					console.log(JSON.stringify(this.addform));
					return false;
				}
      });
    },
    selsChange: function(sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function() {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm("确认删除选中记录吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { ids: ids };
          batchRemoveUser(para).then(res => {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: "删除成功",
              type: "success"
            });
            this.getUsers();
          });
        })
        .catch(() => {});
    }
  },
  mounted() {
    this.getUsers();
  }
};
</script>